package com.sz.sendmsg.controller;

import com.sz.sendmsg.entity.Manager;
import com.sz.sendmsg.vo.ResultVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OtherController {
    @PostMapping("/login")
    public String login(String username,String password){
        System.out.println(username+"--"+password);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try{
            subject.login(token);
            System.out.println(subject.getSession().getId());
        }catch (UnknownAccountException e){
            return "账号错误";
        }catch (IncorrectCredentialsException e){
            return "密码错误";
        }

        return "登录成功";
    }
    @GetMapping("/auth/none")
    public ResultVO authNone(){
        ResultVO resultVO = new ResultVO();
        resultVO.setMsg("NO_AUTH");
        return resultVO;
    }
    @GetMapping("/login/none")
    public ResultVO loginNone(){
        ResultVO resultVO = new ResultVO();
        resultVO.setMsg("NO_LOGIN");
        return resultVO;
    }
    @GetMapping("/get/user")
    public Manager getUser(){
        Manager manager = (Manager) SecurityUtils.getSubject().getPrincipal();
        return manager;
    }
}
